<!DOCTYPE html>
<html>
    <?php
    /*
     * this contains functions to show and edit the orders
     * and create a return
     */
    include_once "../DB/db.php";
    include_once "../class/UserClass.php";

    if ($user->isLoggedOn()) {
        if (isset($_POST['returSubmit'])) { // if the user creates a return
            for ($i = 0; $i < (sizeof($_POST) - 4) / 2; $i++) { // loops trough POST to find number of returns
                $numberof = current($_POST);    // POST contains: number 1=number of times, number 2=itemID+price
                $arr = explode(",", next($_POST));// and so on..
                $skipValue = next($_POST);  // loops trough the returns
                if ($numberof > 0) { // if the item is not to return
                    $sql = 'INSERT INTO returns (oid, vid, uid, price, numberof, sendingdate, problem) values (?, ?, ?, ?, ?, ?, ?)';
                    $sth = $db->prepare($sql);
                    $problem = $_POST['returRadio'] . ' ' . $_POST['description']; // sets the reason for the return
                    $result = $sth->execute(array($_REQUEST['orderitems'], $arr[0], $user->getUid(), $arr[1], $numberof, date("Y-m-d H:m:s"), $problem));
                }   //creates the return
            }
        }
        ?>
        <head>
            <title>Nettbutikk</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link rel="stylesheet" type="text/css" href="../CSS/mainFrame.css" />
            <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
        </head>
        <body>
            <div class="wrapper">
                <?php
                include_once "../MainFrame/header.php";
                include_once "../MainFrame/menuBar.php";
                include_once "../MainFrame/sideBar.php";
                ?>
                <div class="content">
                    <div class="orderContent">
                        <?php
                        if (isset($_REQUEST['orderitems'])) {   // This will display orderitems for one order
                            echo '<h1>Ordre: ' . $_REQUEST['orderitems'] . '</h1>'; // displays what order              creates a return button
                            echo '<a href="showOrders.php?retur=' . $_REQUEST['orderitems'] . '" ><input type="button" name="retur" value="retur av vare" ></a>';
                            $res0 = $db->query('SELECT * from orderitems WHERE oid=' . $_REQUEST['orderitems']);
                            echo '<table id="orderItemTable">'; // create table to display the orderitems
                            echo "<tr><th>Vare ID</th><th>Vare navn</th><th>Antall</th><th>Antall på lager</th><th>Pris</th><th>Sum</th></tr>";
                            $totalprice = 0;
                            while ($row0 = $res0->fetch()) { // displays the values for each orderitem
                                $res1 = $db->query('SELECT * from item WHERE vid=' . $row0['vid']);
                                if ($row1 = $res1->fetch()) {
                                    echo '<tr>';
                                    echo '<td>' . $row1['vid'] . '</td>';
                                    echo '<td><a href="item.php?itemId=' . $row1['vid'] . '" >' . $row1['name'] . '</a></td>';
                                    echo '<td>' . $row0['numberof'] . '</td>';
                                    if ($row1['stock'] == 0) {
                                        echo '<td>Vare ikke på lager</td>';
                                    } else {
                                        echo '<td>' . $row1['stock'] . '</td>';
                                    }
                                    echo '<td>' . $row1['price'] . ',-</td>';
                                    echo '<td>' . $row0['numberof'] * $row1['price'] . ',-</td>';
                                    echo '</tr>';
                                    $totalprice += $row0['numberof'] * $row1['price']; // add up the totalprice
                                }
                            }
                            echo '<tr></tr><tr></tr>'; // lineshifts to look good!
                            echo '<tr id="bottomOrderItemTable"><td></td><td></td><td></td><td></td><td></td><td>Total pris: ' . $totalprice . ',-</td></tr>';
                            echo '</table>'; 
                        } else if (isset($_REQUEST['retur'])) { // if the user want to return som items
                            echo '<h1>Retur av vare</h1>';
                            $res2 = $db->query('SELECT * from orderitems WHERE oid=' . $_REQUEST['retur']);
                            echo '<form name="returForm" method="post" action="showOrders.php?orderitems=' . $_REQUEST['retur'] . '" >';
                            echo '<table id="returItemTable">';// displays the items and a choise to choose how many to return
                            echo "<tr><th>Vare ID</th><th>Vare navn</th><th>Antall</th><th>Pris</th><th>Sum</th><th>Antall retur</th></tr>";
                            $totalprice = 0;
                            $i = 1;
                            while ($row2 = $res2->fetch()) {
                                $res3 = $db->query('SELECT * from item WHERE vid=' . $row2['vid']);
                                if ($row3 = $res3->fetch()) { //displays the ordeitem values
                                    echo '<tr>';
                                    echo '<td>' . $row3['vid'] . '</td>';
                                    echo '<td><a href="item.php?itemId=' . $row3['vid'] . '" >' . $row3['name'] . '</a></td>';
                                    echo '<td>' . $row2['numberof'] . '</td>';
                                    echo '<td>' . $row3['price'] . '</td>';
                                    echo '<td>' . $row2['numberof'] * $row3['price'] . '</td>';
                                    echo '<td><input type="number" name="antRetur' . $i . '" size="4" value="0"></td>';// how many to return
                                    echo '</tr>';
                                    echo '<input type="hidden" name="item' . $i . '" value="' . $row3['vid'] . ',' . $row3['price'] . '" >';
                                    $totalprice += $row2['numberof'] * $row3['price'];
                                    $i++;
                                }
                            }
                            echo '<tr><td></td><td></td><td></td><td></td><td>Total pris: ' . $totalprice . '</td><td></td></tr>';
                            echo '</table>';// choose why the user want to return the item with a description
                            echo '<input type="radio" name="returRadio" value="angre" />Benytt angrefrist<br />';
                            echo '<input type="radio" name="returRadio" value="skade" />Skade på vare / defekt vare<br />';
                            echo '<input type="radio" name="returRadio" value="annet" />Annet<br />';
                            echo '<label>Beskrivelse [254 bokstaver]</label><br><textarea name="description" cols="40" rows="6" ></textarea><br />';
                            echo '<input type="submit" name="returSubmit" >';
                            echo '</form>';
                        } else {
                            if (isset($_REQUEST['aktive'])) { //displays all the orders or only those who are active
                                echo '<h1>Alle dine aktive ordre</h1>';                         //get orders the is not been edited or item is out of stock
                                $res4 = $db->query('SELECT * from orders WHERE sendingdate IN ( \'0000-00-00 00:00:00\' , \'0001-01-01 00:00:00	\' ) AND uid=' . $user->getUid() . ' ORDER BY orderdate DESC'); //
                            } else {
                                echo '<h1>Alle dine ordre</h1>';     //displays all orders                           
                                $res4 = $db->query('SELECT * from orders WHERE uid=' . $user->getUid() . ' ORDER BY orderdate DESC');
                            }
                            echo '<table id="orderTable" >'; //table to list the orders
                            echo "<tr><th>Ordre ID</th><th>Bestillingsdato</th><th>Ordre status</th><th>Total pris</th></tr>";
                            while ($row4 = $res4->fetch()) {
                                echo '<tr onClick="document.location.href=\'showOrders.php?orderitems=' . $row4['oid'] . '\'; " style="cursor:pointer;cursor:hand" >';
                                echo '<td>' . $row4['oid'] . '</td>';   // displays the value for each order
                                echo '<td>' . $row4['orderdate'] . '</td>';
                                if ($row4['sendingdate'] == '0001-01-01 00:00:00') {
                                    echo '<td>Under behandling</td>';
                                } else if ($row4['sendingdate'] == '0000-00-00 00:00:00') { // displays what stat the order is in
                                    echo '<td>vare ikke på lager</td>';
                                } else {
                                    echo '<td>Sendt</td>';
                                }

                                echo '<td>' . $row4['totalprice'] . ',-</td>';
                                echo '</tr>';
                            }
                            echo "</table>";
                        }
                    } else {
                        header("Location: /Views/registrer.php");
                    }
                    ?>
                </div>
            </div>
            <?php
            include_once "../MainFrame/footer.php";
            ?>
        </div>
    </body>
</html>